package edu.ahut.designpattern.chainresponsibility.app;

public class LongFactorialHandler implements FactorialHandler {
    @Override
    public void handleRequest(int n) {
        if (n <= 20) {
            System.err.println("I am " + getClass().getName() + ", I am working with " + n + "!");
            long product = 1;
            for (int i = 2; i <= n; i++) {
                product = product * i;
            }
            System.out.println(n + "!=" + product);
        } else if (nextHandler != null) {
            nextHandler.handleRequest(n);
        }
    }

    @Override
    public void setNextHandler(FactorialHandler nextHandler) {

        this.nextHandler = nextHandler;

    }

    FactorialHandler nextHandler;

}
